﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
    public class SearchBll
    {
        public  List<Course> GetCourses(string type,string value,int sortType,int currenPage,int pageSize,out int pageCount,out int courseNum)
        {
            var context = new ModelContainer();
            int MaxPage = 20*pageSize; 
            switch (type)
            {
                case "1"://院系
                    pageCount = (int)Math.Ceiling((double)context.Courses.Where(course => course.Department == value).Count()/ pageSize);
                    courseNum = context.Courses.Where(course => course.Department == value).Count();
                    if (sortType == 0)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == value)
                            .OrderByDescending(course => course.CourseInfo.Visits)
                            .Skip(pageSize*(currenPage-1))
                            .Take(pageSize)
                            .ToList();
                    if(sortType == 1)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == value)
                            .OrderByDescending(course => course.CourseInfo.Gold)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 2)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == value)
                            .OrderByDescending(course => course.CourseInfo.Relax)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 3)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == value)
                            .OrderByDescending(course => course.CourseInfo.Mark)
                            .Skip(pageSize * (currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    break;
                case "2"://类型
                    pageCount = (int)Math.Ceiling((double)context.Courses.Where(course => course.Type.Contains(value)).Count() / pageSize);
                    courseNum = context.Courses.Where(course => course.Type.Contains(value)).Count();
                    if (sortType == 0)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Type.Contains(value))
                            .OrderByDescending(course => course.CourseInfo.Visits)
                            .Skip(pageSize * (currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                     if (sortType == 1)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Type.Contains(value))
                            .OrderByDescending(course => course.CourseInfo.Gold)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 2)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Type.Contains(value))
                            .OrderByDescending(course => course.CourseInfo.Relax)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 3)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Type.Contains(value))
                            .OrderByDescending(course => course.CourseInfo.Mark)
                            .Skip(pageSize * (currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    break;
                case "3"://英语课
                    pageCount = (int)Math.Ceiling((double)context.Courses.Where(course => course.Department == "外语系" && course.CourseName.Contains("英语")).Count() / pageSize);
                    courseNum =context.Courses.Where(course => course.Department == "外语系" && course.CourseName.Contains("英语")).Count();
                    if (sortType == 0)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "外语系"&& course.CourseName.IndexOf("英语")>-1)
                            .OrderByDescending(course => course.CourseInfo.Visits)
                            .Skip(pageSize*(currenPage-1))
                            .Take(pageSize)
                            .ToList();
                    if(sortType == 1)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "外语系" && course.CourseName.IndexOf("英语") > -1)
                            .OrderByDescending(course => course.CourseInfo.Gold)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 2)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "外语系" && course.CourseName.IndexOf("英语") > -1)
                            .OrderByDescending(course => course.CourseInfo.Relax)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 3)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "外语系" && course.CourseName.IndexOf("英语") > -1)
                            .OrderByDescending(course => course.CourseInfo.Mark)
                            .Skip(pageSize * (currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    break;
                case "4"://体育课
                    pageCount = (int)Math.Ceiling((double)context.Courses.Where(course => course.Department == "体育部").Count() / pageSize);
                    courseNum = context.Courses.Where(course => course.Department == "体育部").Count();
                    if (sortType == 0)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "体育部")
                            .OrderByDescending(course => course.CourseInfo.Visits)
                            .Skip(pageSize*(currenPage-1))
                            .Take(pageSize)
                            .ToList();
                    if(sortType == 1)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "体育部")
                            .OrderByDescending(course => course.CourseInfo.Gold)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 2)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "体育部")
                            .OrderByDescending(course => course.CourseInfo.Relax)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 3)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.Department == "体育部")
                            .OrderByDescending(course => course.CourseInfo.Mark)
                            .Skip(pageSize * (currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    break;
                case "5"://第二外国语
                    pageCount = (int)Math.Ceiling((double)context.Courses.Where(course => course.CourseName.Contains("第二外国语")).Count() / pageSize);
                    courseNum = context.Courses.Where(course => course.CourseName.Contains("第二外国语")).Count();
                    if (sortType == 0)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.IndexOf("第二外国语")>-1)
                            .OrderByDescending(course => course.CourseInfo.Visits)
                            .Skip(pageSize*(currenPage-1))
                            .Take(pageSize)
                            .ToList();
                    if(sortType == 1)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.IndexOf("第二外国语") > -1)
                            .OrderByDescending(course => course.CourseInfo.Gold)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 2)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.IndexOf("第二外国语") > -1)
                            .OrderByDescending(course => course.CourseInfo.Relax)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 3)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.IndexOf("第二外国语") > -1)
                            .OrderByDescending(course => course.CourseInfo.Mark)
                            .Skip(pageSize * (currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    break;
                case "6"://围观最多人->推荐最多人
                    var resultV = context.Courses
                        .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                        .OrderByDescending(course => course.CourseInfo.RcmdNum).Take(MaxPage);
                    pageCount = (int)Math.Ceiling((double)resultV.Count() / pageSize);
                    courseNum = resultV.Count();
                    return 
                        resultV.Skip(pageSize*(currenPage - 1))
                        .Take(pageSize)
                        .ToList();
                    break;
                case "7"://含金量最高
                    var resultG = context.Courses
                        .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                        .OrderByDescending(course => course.CourseInfo.Gold)
                        .ThenByDescending(course => course.CourseInfo.Visits)
                        .Take(MaxPage);
                    pageCount = (int)Math.Ceiling((double)resultG.Count() / pageSize);
                    courseNum = resultG.Count();
                    return 
                       resultG.Skip(pageSize * (currenPage - 1))
                        .Take(pageSize)
                        .ToList();
                    break;
                case "8"://给分最厚道
                    var resultM = context.Courses
                        .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                        .OrderByDescending(course => course.CourseInfo.Mark)
                        .ThenByDescending(course => course.CourseInfo.Visits)
                        .Take(MaxPage);
                    pageCount = (int)Math.Ceiling((double)resultM.Count() / pageSize);
                    courseNum = resultM.Count();
                    return
                        resultM.Skip(pageSize * (currenPage - 1))
                        .Take(pageSize)
                        .ToList();
                    break;
                case "10"://所搜结果
                    var result = context.Courses.Include("Teacher").Include("CourseInfo").Include("CoursePlaces").AsEnumerable();
                    string[] valueAll = value.Split(' ');
                    foreach (var v in valueAll)
                    {
                        result = result.Where(course =>
                            course.CourseName.Contains(v)
                            || course.Teacher.TeacherName.Contains(v)
                            || course.Type.Contains(v)
                            || course.Department.Contains(v)
                            || course.CourseId.Contains(v));
                    }
                    result = result.Take(MaxPage);
                    pageCount = (int)Math.Ceiling((double)result.Count() / pageSize);
                    courseNum = result.Count();
                    result=result.Skip(pageSize*(currenPage - 1)).Take(pageSize);
                   
                    return result.ToList();
                    break;
                case "11"://指定课程名的课程
                    pageCount = (int)Math.Ceiling((double)context.Courses.Where(course => course.CourseName.Equals(value)).Count() / pageSize);
                    courseNum = context.Courses.Where(course => course.CourseName.Equals(value)).Count();
                    if (sortType == 0)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.Equals(value))
                            .OrderByDescending(course => course.CourseInfo.Visits)
                            .Skip(pageSize*(currenPage-1))
                            .Take(pageSize)
                            .ToList();
                    if(sortType == 1)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.Equals(value))
                            .OrderByDescending(course => course.CourseInfo.Gold)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 2)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.Equals(value))
                            .OrderByDescending(course => course.CourseInfo.Relax)
                            .Skip(pageSize*(currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    if (sortType == 3)
                        return context.Courses
                            .Include("CourseInfo").Include("Teacher").Include("CoursePlaces")
                            .Where(course => course.CourseName.Equals(value))
                            .OrderByDescending(course => course.CourseInfo.Mark)
                            .Skip(pageSize * (currenPage - 1))
                            .Take(pageSize)
                            .ToList();
                    break;

                  
            }
            pageCount = 0;
            courseNum = 0;
            return context.Courses
                .Include("Teacher").Include("CoursePlaces").Include("CourseInfo")
                .OrderBy(course => course.CourseInfo.Visits)
                .Skip(pageSize * (currenPage - 1))
                .Take(pageSize)
                .ToList();
        }
    }
}
